Imports System.Data.SqlClient


Public Class frmAPAgeingReport

    Inherits System.Windows.Forms.Form
    Dim util As New AppsUtility.EncDec
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents GroupPanel1 As DevComponents.DotNetBar.Controls.GroupPanel
    Friend WithEvents cmbGroup As DevComponents.DotNetBar.Controls.ComboBoxEx
    Friend WithEvents cmbCompany As DevComponents.DotNetBar.Controls.ComboBoxEx
    Friend WithEvents cmdRefresh As DevComponents.DotNetBar.ButtonX
    Friend WithEvents cmdPrint As DevComponents.DotNetBar.ButtonX
    Friend WithEvents cmdExit As DevComponents.DotNetBar.ButtonX
    Public strCon As String = util.toDecrypt(System.Configuration.ConfigurationSettings.AppSettings("SQLString"))

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents dgArLedger As System.Windows.Forms.DataGrid
    Friend WithEvents dtpAge As System.Windows.Forms.DateTimePicker
    Friend WithEvents aPageingRpt1 As APageingRpt
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.dtpAge = New System.Windows.Forms.DateTimePicker()
        Me.dgArLedger = New System.Windows.Forms.DataGrid()
        Me.aPageingRpt1 = New APageingRpt()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.GroupPanel1 = New DevComponents.DotNetBar.Controls.GroupPanel()
        Me.cmdRefresh = New DevComponents.DotNetBar.ButtonX()
        Me.cmdPrint = New DevComponents.DotNetBar.ButtonX()
        Me.cmdExit = New DevComponents.DotNetBar.ButtonX()
        Me.cmbGroup = New DevComponents.DotNetBar.Controls.ComboBoxEx()
        Me.cmbCompany = New DevComponents.DotNetBar.Controls.ComboBoxEx()
        CType(Me.dgArLedger, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.GroupPanel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.BackColor = System.Drawing.Color.Transparent
        Me.Label1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
        Me.Label1.Location = New System.Drawing.Point(6, 20)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(83, 18)
        Me.Label1.TabIndex = 47
        Me.Label1.Text = "Ageing Date"
        '
        'dtpAge
        '
        Me.dtpAge.Format = System.Windows.Forms.DateTimePickerFormat.[Short]
        Me.dtpAge.Location = New System.Drawing.Point(13, 65)
        Me.dtpAge.Name = "dtpAge"
        Me.dtpAge.Size = New System.Drawing.Size(120, 26)
        Me.dtpAge.TabIndex = 154
        '
        'dgArLedger
        '
        Me.dgArLedger.BackgroundColor = System.Drawing.Color.AliceBlue
        Me.dgArLedger.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.dgArLedger.CaptionVisible = False
        Me.dgArLedger.DataMember = ""
        Me.dgArLedger.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.dgArLedger.Location = New System.Drawing.Point(13, 101)
        Me.dgArLedger.Name = "dgArLedger"
        Me.dgArLedger.PreferredColumnWidth = 175
        Me.dgArLedger.ReadOnly = True
        Me.dgArLedger.Size = New System.Drawing.Size(921, 311)
        Me.dgArLedger.TabIndex = 249
        Me.dgArLedger.TabStop = False
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.BackColor = System.Drawing.Color.Transparent
        Me.Label2.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
        Me.Label2.Location = New System.Drawing.Point(335, 21)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(66, 18)
        Me.Label2.TabIndex = 47
        Me.Label2.Text = "Company"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.BackColor = System.Drawing.Color.Transparent
        Me.Label3.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
        Me.Label3.Location = New System.Drawing.Point(134, 21)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(101, 18)
        Me.Label3.TabIndex = 47
        Me.Label3.Text = "Supplier Group"
        '
        'GroupPanel1
        '
        Me.GroupPanel1.CanvasColor = System.Drawing.SystemColors.Control
        Me.GroupPanel1.ColorSchemeStyle = DevComponents.DotNetBar.eDotNetBarStyle.Office2007
        Me.GroupPanel1.Controls.Add(Me.cmdRefresh)
        Me.GroupPanel1.Controls.Add(Me.cmdPrint)
        Me.GroupPanel1.Controls.Add(Me.cmdExit)
        Me.GroupPanel1.Controls.Add(Me.Label3)
        Me.GroupPanel1.Controls.Add(Me.Label2)
        Me.GroupPanel1.Controls.Add(Me.cmbGroup)
        Me.GroupPanel1.Controls.Add(Me.cmbCompany)
        Me.GroupPanel1.Controls.Add(Me.Label1)
        Me.GroupPanel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.GroupPanel1.Location = New System.Drawing.Point(0, 0)
        Me.GroupPanel1.Name = "GroupPanel1"
        Me.GroupPanel1.Size = New System.Drawing.Size(954, 476)
        '
        '
        '
        Me.GroupPanel1.Style.BackColor2SchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground2
        Me.GroupPanel1.Style.BackColorGradientAngle = 90
        Me.GroupPanel1.Style.BackColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBackground
        Me.GroupPanel1.Style.BorderBottom = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderBottomWidth = 1
        Me.GroupPanel1.Style.BorderColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelBorder
        Me.GroupPanel1.Style.BorderLeft = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderLeftWidth = 1
        Me.GroupPanel1.Style.BorderRight = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderRightWidth = 1
        Me.GroupPanel1.Style.BorderTop = DevComponents.DotNetBar.eStyleBorderType.Solid
        Me.GroupPanel1.Style.BorderTopWidth = 1
        Me.GroupPanel1.Style.CornerDiameter = 4
        Me.GroupPanel1.Style.CornerType = DevComponents.DotNetBar.eCornerType.Rounded
        Me.GroupPanel1.Style.TextAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Center
        Me.GroupPanel1.Style.TextColorSchemePart = DevComponents.DotNetBar.eColorSchemePart.PanelText
        Me.GroupPanel1.Style.TextLineAlignment = DevComponents.DotNetBar.eStyleTextAlignment.Near
        Me.GroupPanel1.TabIndex = 250
        Me.GroupPanel1.Text = "AP Ageing Report"
        '
        'cmdRefresh
        '
        Me.cmdRefresh.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton
        Me.cmdRefresh.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdRefresh.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(64, Byte), Integer))
        Me.cmdRefresh.Location = New System.Drawing.Point(761, 408)
        Me.cmdRefresh.Name = "cmdRefresh"
        Me.cmdRefresh.Size = New System.Drawing.Size(82, 27)
        Me.cmdRefresh.TabIndex = 255
        Me.cmdRefresh.Text = "Refresh"
        '
        'cmdPrint
        '
        Me.cmdPrint.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton
        Me.cmdPrint.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdPrint.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(64, Byte), Integer))
        Me.cmdPrint.Location = New System.Drawing.Point(673, 408)
        Me.cmdPrint.Name = "cmdPrint"
        Me.cmdPrint.Size = New System.Drawing.Size(82, 27)
        Me.cmdPrint.TabIndex = 254
        Me.cmdPrint.Text = "Print"
        '
        'cmdExit
        '
        Me.cmdExit.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton
        Me.cmdExit.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdExit.ForeColor = System.Drawing.Color.FromArgb(CType(CType(0, Byte), Integer), CType(CType(0, Byte), Integer), CType(CType(64, Byte), Integer))
        Me.cmdExit.Location = New System.Drawing.Point(849, 408)
        Me.cmdExit.Name = "cmdExit"
        Me.cmdExit.Size = New System.Drawing.Size(82, 27)
        Me.cmdExit.TabIndex = 253
        Me.cmdExit.Text = "Exit"
        '
        'cmbGroup
        '
        Me.cmbGroup.DisplayMember = "Text"
        Me.cmbGroup.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        Me.cmbGroup.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cmbGroup.ItemHeight = 19
        Me.cmbGroup.Location = New System.Drawing.Point(135, 42)
        Me.cmbGroup.Name = "cmbGroup"
        Me.cmbGroup.Size = New System.Drawing.Size(195, 25)
        Me.cmbGroup.TabIndex = 48
        '
        'cmbCompany
        '
        Me.cmbCompany.DisplayMember = "Text"
        Me.cmbCompany.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        Me.cmbCompany.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cmbCompany.ItemHeight = 19
        Me.cmbCompany.Location = New System.Drawing.Point(338, 42)
        Me.cmbCompany.Name = "cmbCompany"
        Me.cmbCompany.Size = New System.Drawing.Size(244, 25)
        Me.cmbCompany.TabIndex = 48
        '
        'APageingReport
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(7, 19)
        Me.BackColor = System.Drawing.SystemColors.ActiveCaption
        Me.ClientSize = New System.Drawing.Size(954, 476)
        Me.ControlBox = False
        Me.Controls.Add(Me.dtpAge)
        Me.Controls.Add(Me.dgArLedger)
        Me.Controls.Add(Me.GroupPanel1)
        Me.Font = New System.Drawing.Font("Calibri", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ForeColor = System.Drawing.SystemColors.ControlLightLight
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Location = New System.Drawing.Point(50, 50)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "APageingReport"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        CType(Me.dgArLedger, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupPanel1.ResumeLayout(False)
        Me.GroupPanel1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim sqlCon As SqlConnection = New SqlConnection(strCon)
    Dim strsql As String
    Dim sqlCmd As SqlCommand = New SqlCommand(strsql, sqlCon)
    Dim rdr As SqlDataReader
    Dim OldTodate, OldFrdate As String
    Dim Bqty As Integer
    Dim FrDt, ToDt As String
    Private Sub Budgets_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim APdata As New APdataLogic
        Dim ds As DataSet
        ds = APdata.GetAllCompany()
        cmbCompany.DataSource = ds.Tables(0)
        cmbCompany.ValueMember = "CompanyID"
        cmbCompany.DisplayMember = "CompanyName"
        cmbCompany.SelectedValue = CompanyID
        LoadGroupCombos(CompanyID)
        '
    End Sub
    Private Sub LoadGroupCombos(ByVal CompanyCode As Integer)
        Dim APdata As New APdataLogic
        Dim ds As DataSet

        ds = APdata.GetSupplierGroups()
        cmbGroup.DataSource = ds.Tables(0)
        cmbGroup.ValueMember = "GroupID"
        cmbGroup.DisplayMember = "GroupName"
        cmbGroup.SelectedValue = 0
        '
        APdata = Nothing
    End Sub
    Private Sub dgArLedger_load()
        Try
            Dim FrDt, ToDt As String
            FrDt = dtpAge.Text
            FrDt = FrDt.Substring(6, 4) & "-" & FrDt.Substring(3, 2) & "-" & FrDt.Substring(0, 2)
            Dim WhichCompany As String = " "
            'If cmbCompany.SelectedValue > 1 Then
            '    WhichCompany = " and CompanyID=" & cmbCompany.SelectedValue & " "
            'End If
            strsql = "select * from " & _
                    "(select a.SupCode,a.SupName,sum(balance) balance,sum(DrBal) DrBal," & _
                    "sum(within30) within30,sum(over30) over30,sum(over60) over60,sum(over90) over90 " & _
                    "from Supplier a," & _
                    "(select supid,sum(docamt) over90,0 over60,0 over30,0 within30,0 balance,0 DrBal " & _
                    "from aptrx where(DateDiff(Day, docdate, getdate()) > 90) " & _
                    "and DocAmt<0 and docdate<='" & FrDt & "' " & WhichCompany & _
                    "group by supid " & _
                    "union all " & _
                    "select supid,0 over90,sum(docamt) over60,0 over30,0 within30,0 balance,0 DrBal " & _
                    "from aptrx where datediff(day,docdate,getdate())<=90 and datediff(day,docdate,getdate())>60 " & _
                    "and DocAmt<0 and docdate<='" & FrDt & "' " & WhichCompany & _
                    "group by supid " & _
                    "union all " & _
                    "select supid,0 over90,0 over60,sum(docamt) over30,0 within30,0 balance,0 DrBal " & _
                    "from aptrx " & _
                    "where datediff(day,docdate,getdate())<=60 and datediff(day,docdate,getdate())>30 " & _
                    "and DocAmt<0 and docdate<='" & FrDt & "' " & WhichCompany & _
                    "group by supid " & _
                    "union all " & _
                    "select supid,0 over90,0 over60,0 over30,0 within30,sum(docamt) balance,0 DrBal " & _
                    "from aptrx " & _
                    "where docdate<='" & FrDt & "' " & WhichCompany & _
                    "group by supid " & _
                    "union all " & _
                    "select supid,0 over90,0 over60,0 over30,0 within30,0 balance,sum(DocAmt) DrBal " & _
                    "from aptrx " & _
                    "where DocAmt>0 and docdate<='" & FrDt & "' " & WhichCompany & _
                    "group by supid " & _
                    "union all " & _
                    "select supid,0 over90,0 over60,0 over30,sum(docamt) within30,0 balance,0 DrBal " & _
                    "from aptrx where datediff(day,docdate,getdate())<=30 " & _
                    "and DocAmt<0 and docdate<='" & FrDt & "' " & WhichCompany & _
                    "group by supid) b " & _
                    "where a.SupID=b.SupID " & _
                    "group by a.SupCode,a.SupName) c " & _
                    "where Balance<>0 or Over30<>0 or Over60<>0 or Over90<>0 or within30<>0 or DrBal<>0 " & _
                    "order by SupName"

            Dim da As New SqlClient.SqlDataAdapter(strsql, sqlCon)
            Dim ds As New DataSet("TRENQ")
            da.Fill(ds, "TRENQ")
            Dim Bal, Day30, Day60, Day90, DayOver90 As Double
            Dim drow As DataRow
            For Each drow In ds.Tables(0).Rows
                Dim over90, over60, over30, within30, drbal As Decimal
                over90 = drow("Over90")
                over60 = drow("Over60")
                over30 = drow("Over30")
                within30 = drow("Within30")
                drbal = drow("DrBal").ToString().Replace("-", "")
                If drbal > 0 Then
                    If over90 <> 0 Then
                        If over90.ToString().Replace("-", "") >= drbal Then
                            over90 = over90 + drbal
                            drbal = 0
                        Else
                            drbal = over90 + drbal
                            over90 = 0
                        End If
                    End If
                End If
                If drbal > 0 Then
                    If over60.ToString().Replace("-", "") >= drbal Then
                        over60 = over60 + drbal
                        drbal = 0
                    Else
                        drbal = drbal + over60
                        over60 = 0
                    End If
                End If
                If drbal > 0 Then
                    If over30.ToString().Replace("-", "") >= drbal Then
                        over30 = over30 + drbal
                        drbal = 0
                    Else
                        drbal = drbal + over30
                        over30 = 0
                    End If
                End If
                If drbal > 0 Then
                    If within30.ToString().Replace("-", "") >= drbal Then
                        within30 = within30 + drbal
                        drbal = 0
                    Else
                        drbal = drbal + within30
                        within30 = 0
                    End If
                End If
                Bal += drow("balance")
                Day30 += within30
                Day60 += over30
                Day90 += over60
                DayOver90 += over90
                If drow("balance") <= 0 Then
                    drow("balance") = drow("balance").ToString.Replace("-", "")
                Else
                    drow("balance") = 0 - drow("balance")
                End If
                If within30 <= 0 Then
                    drow("Within30") = within30.ToString.Replace("-", "")
                Else
                    drow("Within30") = 0 - within30
                End If
                If over30 <= 0 Then
                    drow("Over30") = over30.ToString.Replace("-", "")
                Else
                    drow("Over30") = 0 - over30
                End If
                If over60 <= 0 Then
                    drow("Over60") = over60.ToString.Replace("-", "")
                Else
                    drow("Over60") = 0 - over60
                End If
                If over90 <= 0 Then
                    drow("Over90") = over90.ToString.Replace("-", "")
                Else
                    drow("Over90") = 0 - over90
                End If
            Next
            If Bal <= 0 Then
                Bal = CDbl(Bal.ToString.Replace("-", ""))
            Else
                Bal = 0 - Bal
            End If
            If Day30 <= 0 Then
                Day30 = CDbl(Day30.ToString.Replace("-", ""))
            Else
                Day30 = 0 - Day30
            End If
            If Day60 <= 0 Then
                Day60 = CDbl(Day60.ToString.Replace("-", ""))
            Else
                Day60 = 0 - Day60
            End If
            If Day90 <= 0 Then
                Day90 = CDbl(Day90.ToString.Replace("-", ""))
            Else
                Day90 = 0 - Day90
            End If
            If DayOver90 <= 0 Then
                DayOver90 = CDbl(DayOver90.ToString.Replace("-", ""))
            Else
                DayOver90 = 0 - DayOver90
            End If

            drow = ds.Tables(0).NewRow()
            drow("SupName") = "TOTAL:"
            drow("Balance") = Bal.ToString("#0.000")
            drow("Within30") = Day30.ToString("#0.000")
            drow("Over30") = Day60.ToString("#0.000")
            drow("Over60") = Day90.ToString("#0.000")
            drow("Over90") = DayOver90.ToString("#0.000")
            ds.Tables(0).Rows.Add(drow)

            dgArLedger.DataSource = ds
            dgArLedger.DataMember = "TRENQ"
            dgArLedger.ReadOnly = True
            dgArLedger.TableStyles.Clear()
            dgArLedger_Style()
            sqlCon.Close()
            '
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub dgArLedger_Style()
        With dgArLedger
            .TableStyles.Clear()
            .CaptionBackColor = System.Drawing.Color.SlateBlue
            .CaptionForeColor = System.Drawing.Color.LemonChiffon
            .AllowSorting = True
            .AlternatingBackColor = System.Drawing.Color.AliceBlue
            .ParentRowsBackColor = System.Drawing.Color.Lavender
            .ParentRowsForeColor = System.Drawing.Color.SlateBlue
            .HeaderBackColor = System.Drawing.Color.LemonChiffon
            .AllowNavigation = False
            .ParentRowsVisible = False
            .RowHeadersVisible = False
        End With

        Dim grdTableStyle As New DataGridTableStyle
        With grdTableStyle
            .AlternatingBackColor = Color.AliceBlue
            .HeaderFont = New Font("Calibri", 9.0!, FontStyle.Bold)
            .MappingName = "TRENQ"
            .PreferredColumnWidth = 160
            .HeaderBackColor = Color.LightSlateGray
            .HeaderForeColor = Color.LightCyan
            .PreferredRowHeight = 15
            .RowHeadersVisible = False
            .RowHeaderWidth = 0
        End With

        'Dim cs As DataGridColumnStyle
        'cs = New DataGridTextBoxColumn
        'cs.Width = 60
        'cs.MappingName = "DocCus"
        'cs.HeaderText = "Code"
        'cs.NullText = ""
        'grdTableStyle.GridColumnStyles.Add(cs)

        Dim cs1 As DataGridColumnStyle
        cs1 = New DataGridTextBoxColumn
        cs1.Width = 290
        cs1.MappingName = "SupName"
        cs1.HeaderText = "Supplier Name"
        cs1.NullText = ""
        grdTableStyle.GridColumnStyles.Add(cs1)

        Dim cs7 As DataGridColumnStyle
        cs7 = New DataGridTextBoxColumn  ' Add the standard column style.
        cs7.Width = 120
        cs7.MappingName = "Balance"
        cs7.HeaderText = "Balance"
        cs7.NullText = ""
        cs7.ReadOnly = True
        cs7.Alignment = HorizontalAlignment.Right
        grdTableStyle.GridColumnStyles.Add(cs7)

        Dim cs2 As DataGridColumnStyle
        cs2 = New DataGridTextBoxColumn  ' Add the standard column style.
        cs2.Width = 120
        cs2.MappingName = "Within30"
        cs2.HeaderText = "Current"
        cs2.NullText = ""
        cs2.ReadOnly = True
        cs2.Alignment = HorizontalAlignment.Right
        grdTableStyle.GridColumnStyles.Add(cs2)

        Dim cs3 As DataGridColumnStyle
        cs3 = New DataGridTextBoxColumn  ' Add the standard column style.
        cs3.Width = 120
        cs3.MappingName = "Over30"
        cs3.HeaderText = "Over 30 Days"
        cs3.NullText = ""
        cs3.ReadOnly = True
        cs3.Alignment = HorizontalAlignment.Right
        grdTableStyle.GridColumnStyles.Add(cs3)

        Dim cs4 As DataGridColumnStyle
        cs4 = New DataGridTextBoxColumn  ' Add the standard column style.
        cs4.Width = 120
        cs4.MappingName = "Over60"
        cs4.HeaderText = "Over 60 Days"
        cs4.NullText = ""
        cs4.ReadOnly = True
        cs4.Alignment = HorizontalAlignment.Right
        grdTableStyle.GridColumnStyles.Add(cs4)

        Dim cs5 As DataGridColumnStyle
        cs5 = New DataGridTextBoxColumn  ' Add the standard column style.
        cs5.Width = 120
        cs5.MappingName = "Over90"
        cs5.HeaderText = "Over 90 Days"
        cs5.NullText = ""
        cs5.ReadOnly = True
        cs5.Alignment = HorizontalAlignment.Right
        grdTableStyle.GridColumnStyles.Add(cs5)

        dgArLedger.TableStyles.Add(grdTableStyle)

    End Sub



    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Hide()
    End Sub

    Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click
        dgArLedger_load()
    End Sub

    Private Sub dgArLedger_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgArLedger.MouseUp
        Dim pt As New Point(e.X, e.Y)
        Dim hti As DataGrid.HitTestInfo = dgArLedger.HitTest(pt)
        If hti.Type = DataGrid.HitTestType.Cell Then
            dgArLedger.CurrentCell = New DataGridCell(hti.Row, hti.Column)
            dgArLedger.Select(hti.Row)
        End If
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        Dim ds As DataSet = New DataSet("SerpsRpt")
        Dim table As DataTable = New DataTable("SerpsRpt")
        table.Columns.Add("Str1")
        table.Columns.Add("Dec1")
        table.Columns.Add("Dec2")
        table.Columns.Add("Dec3")
        table.Columns.Add("Dec4")
        table.Columns.Add("Dec5")
        Dim mrow, drow As Integer
        Dim cm As CurrencyManager = BindingContext(dgArLedger.DataSource, dgArLedger.DataMember)
        Dim dv As DataView = cm.List
        dv.RowStateFilter = DataViewRowState.CurrentRows
        mrow = dv.Count
        Dim n As Integer
        For n = 0 To mrow - 1
            Dim nrow As DataRow = table.NewRow()
            nrow("Str1") = dgArLedger.Item(n, 0)
            nrow("Dec1") = dgArLedger.Item(n, 1)
            nrow("Dec2") = dgArLedger.Item(n, 2)
            nrow("Dec3") = dgArLedger.Item(n, 3)
            nrow("Dec4") = dgArLedger.Item(n, 4)
            nrow("Dec5") = dgArLedger.Item(n, 5)
            table.Rows.Add(nrow)
        Next
        ds.Tables.Add(table)

        aPageingRpt1.Load()
        aPageingRpt1.SetDataSource(ds)

        aPageingRpt1.SetParameterValue("CompanyName", CompanyTitle)
        aPageingRpt1.SetParameterValue("ReportTitle", "A/P Ageing Report")
        aPageingRpt1.SetParameterValue("AgeingDate", dtpAge.Text)

        If PrntPrv Is Nothing Then
            PrntPrv = New PrintPreviewForm
            PrntPrv.CrRptView.ReportSource = aPageingRpt1
            PrntPrv.ShowDialog()
        End If
        aPageingRpt1.Close()
        'apageingrpt1.PrintOptions.PrinterName = InvoicePrinter
        'Try
        '    apageingrpt1.PrintToPrinter(1, True, 0, 0)
        'Catch ex As Exception
        '    MsgBox(ex.ToString)
        'End Try
    End Sub
End Class
